package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciRateDTO;
import com.zatech.cgnci.project.dto.RateQueryDTO;
import com.zatech.cgnci.project.entity.CgnciRateDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.mapper.CgnciRateMapper;
import com.zatech.cgnci.project.service.CgnciCodeTypeService;
import com.zatech.cgnci.project.service.CgnciRateService;
import com.zatech.cgnci.project.vo.RateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 汇率信息表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-01-13
 */
@Service
public class CgnciRateServiceImpl extends ServiceImpl<CgnciRateMapper, CgnciRateDO> implements CgnciRateService {

    @Resource
    private CgnciCodeTypeService cgnciCodeTypeService;

    @Override
    public IPage<RateVO> pageRate(RateQueryDTO query) {
        return this.baseMapper.pageRate(query.toPage(), query);
    }


    @Override
    public void insertRate(CgnciRateDTO cgnciRateDTO) {
        UserEntity user = UserContext.getUser();
        // 校验原币和目标币在有效期间，状态为已启用的数据是否存在
        CgnciRateDO rateDO = this.getOne(new LambdaQueryWrapper<CgnciRateDO>()
                .eq(CgnciRateDO::getBaseCurrency, cgnciRateDTO.getBaseCurrency())
                .eq(CgnciRateDO::getTargetCurrency, cgnciRateDTO.getTargetCurrency())
                .eq(CgnciRateDO::getDisabledStatus, 0)
                // 当前日期大于等于生效日期
                .le(CgnciRateDO::getEffectiveDate, new Date())
                // 当前日期小于等于失效日期
                .ge(CgnciRateDO::getExpirationDate, new Date()));
        if (VerifyUtils.isNotEmpty(rateDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "当前币种已存在,不能新增");
        }
        CgnciRateDO cgnciRateDO = new CgnciRateDO();
        BeanUtils.copyProperties(cgnciRateDTO, cgnciRateDO);
        cgnciRateDO.setCreatedBy(user.getNameAndCode());
        this.baseMapper.insert(cgnciRateDO);
    }

    @Override
    public void updateRate(CgnciRateDTO cgnciRateDTO) {
        if (VerifyUtils.isEmpty(cgnciRateDTO.getId())) {
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "id为空");
        }

        CgnciRateDO rateDO = this.getById(cgnciRateDTO.getId());
        if (VerifyUtils.isEmpty(rateDO)) {
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "id有误");
        }

        // 校验原币和目标币在有效期间，状态为已启用的数据是否存在
        CgnciRateDO checkRateDO = this.getOne(new LambdaQueryWrapper<CgnciRateDO>()
                .notIn(CgnciRateDO::getId, rateDO.getId())
                .eq(CgnciRateDO::getBaseCurrency, cgnciRateDTO.getBaseCurrency())
                .eq(CgnciRateDO::getTargetCurrency, cgnciRateDTO.getTargetCurrency())
                .eq(CgnciRateDO::getDisabledStatus, 0)
                // 当前日期大于等于生效日期
                .le(CgnciRateDO::getEffectiveDate, new Date())
                // 当前日期小于等于失效日期
                .ge(CgnciRateDO::getExpirationDate, new Date()));
        if (VerifyUtils.isNotEmpty(checkRateDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "当前币种已配置,请修改原有的配置信息");
        }
        CgnciRateDO cgnciRateDO = new CgnciRateDO();
        BeanUtils.copyProperties(cgnciRateDTO, cgnciRateDO);
        this.baseMapper.updateById(cgnciRateDO);
    }

    @Override
    public void deleteRate(Long rateId) {
        this.baseMapper.deleteByRateId(rateId);
    }

    @Override
    public RateVO getRateByTargetCurrency(String targetCurrency) {
        // 查询目标币对人民币的汇率
        CgnciRateDO rateDO = this.getOne(new LambdaQueryWrapper<CgnciRateDO>()
                .eq(CgnciRateDO::getBaseCurrency, "CNY")
                .eq(CgnciRateDO::getTargetCurrency, targetCurrency)
                .eq(CgnciRateDO::getDisabledStatus, 0)
                // 当前日期大于等于生效日期
                .le(CgnciRateDO::getEffectiveDate, new Date())
                // 当前日期小于等于失效日期
                .ge(CgnciRateDO::getExpirationDate, new Date()));
        if (VerifyUtils.isEmpty(rateDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "请配置人民币对" + targetCurrency + "的汇率");
        }
        return ConvertUtils.convert(rateDO, RateVO.class);
    }
}
